var DataGrid = function (config) {
	config = config || {};
	var dataGrid = config.dataGrid || {}
	// Actions
	var actionUrl = config.action || {}
	var Action = {
		'add_editForm': actionUrl.add_editForm || 'form.html',
		'save': actionUrl.save || 'save',
		'getId': actionUrl.getId || 'getId',
		'remove': actionUrl.remove || 'delete'
	}
	var wid_height = config.wid_height || {}
	var widheight = {
		'width': wid_height.width || 800,
		'height': wid_height.height || 400,
	}
	// Grid DataList
	var Grid = $('#data-list');
	// Form
	var Form = {
		search: $("#searchForm"),
		edit: $("#editForm")
	}
	// Win 窗口
	var Win = {
		edit: $("#edit-win")
	}

	// 处理函数
	var Handler = {
		// serach 查询事件
		search: function (callback) {
			//var startTimeSearch = $("#startTimeSearch").val();
			//console.info(startTimeSearch);
			Events.refresh();
			// 回调函数
			if (jQuery.isFunction(callback)) {
				callback();
			}
			return false;
		},
		// add按钮事件
		add: function (callback) {
			Win.edit.dialog({
				href: path + Action.add_editForm,
				width: widheight.width,
				height: widheight.height,
				modal: true,
				resizable: true,
			/*	collapsible: true,
				maximizable: true,*/
				onLoad: function () {
					$("#editForm").resetForm();
				},
				title: '添加',
				closable: true,
				buttons: [{
					text: '保存',
					iconCls: 'fa fa-save',
					btnCls: 'topjui-btn',
					handler: Events.save
				}/*, {
					text: "关闭",
					iconCls: "fa fa-close",
					btnCls: "topjui-btn-danger",
					handler: Events.close
				}*/],
			});
			// 回调函数
			if (jQuery.isFunction(callback)) {
				callback();
			}
		},
		// edit 按钮事件
		edit: function (callback) {
			var record = Utils.getCheckedRows();
			if (Utils.checkSelectOne(record)) {
				Bdth.progress();
				var data = {};
				var idKey = dataGrid.idField || 'id'; // 主键名称
				data[idKey] = (record[0][idKey]);
				Bdth.getById(Action.getId, data, function (result) {
					Bdth.closeProgress();
					Win.edit.dialog({
						href: path + Action.add_editForm,
						width: widheight.width,
						height: widheight.height,
						modal: true,
						resizable: true,
					/*	collapsible: true,
						maximizable: true,*/
						onLoad: function () {
							$("#editForm").form('load', result.data);
							$("#edit-department").removeClass('depInputClass');
						},
						title: '编辑',
						buttons: [{
							text: '保存',
							iconCls: 'fa fa-save',
							btnCls: 'topjui-btn',
							handler: Events.save
						}/*, {
							text: "关闭",
							iconCls: "fa fa-close",
							btnCls: "topjui-btn-danger",
							handler: Events.close
						}*/],
					});

					// 回调函数
					if (jQuery.isFunction(callback)) {
						callback(result);
					}

				});
			}
		},
		// 刷新Grid 数据
		refresh: function (callback) {
			var param = Form.search.serializeObject();
			Grid.datagrid('reload', param);
			// 回调函数
			if (jQuery.isFunction(callback)) {
				callback();
			}
		},
		// 删除记录
		remove: function (callback) {
			var records = Utils.getCheckedRows();
			if (Utils.checkSelect(records)) {
				$.iMessager.confirm('提示', '你确定要删除?', function (r) {
					if (r) {
						Bdth.progress();
						var arr = [], idKey = dataGrid.idField || 'id'; // 主键名称
						$.each(records, function (i, record) {
							arr.push('id=' + record[idKey]);
						});
						var data = arr.join("&");
						Bdth.deleteForm(Action.remove, data, function (result) {
							Bdth.closeProgress();
							Events.refresh();
							// 回调函数
							if (jQuery.isFunction(callback)) {
								callback(result);
							}
						});
					}
				});
			}
		},// 保存调用方法
		save: function (callback) {
			if ($("#editForm").form('validate')) {
				Bdth.progress();
				$("#editForm").attr('action', Action.save);
				var startTime = $("#startTime").val();
				var endTime = $("#endTime").val();
				var data = {
					"startTimeHidden": startTime,
					"endTimeHidden": endTime
				};//附加的数据
				Bdth.saveForm($("#editForm"), data, function (data) {
					Bdth.closeProgress();
					Win.edit.dialog('close');
					Events.refresh();
					$("#editForm").resetForm();
					// 回调函数
					if (jQuery.isFunction(callback)) {
						callback(data);
					}
				});
			}
		},
		// 关闭按钮事件
		close: function (callback) {
			$.iMessager.confirm('提示', '你确定要关闭窗口?', function (r) {
				if (r) {
					Win.edit.dialog('close');
					// 回调函数
					if (jQuery.isFunction(callback)) {
						callback(data);
					}
				}
			});
		}
	}

	// Grid 工具类
	var Utils = {
		getCheckedRows: function () {
			return Grid.datagrid('getChecked');
		},
		checkSelect: function (rows) {// 检查grid是否有勾选的行, 有返回 true,没有返回true
			var records = rows;
			if (records && records.length > 0) {
				return true;
			}
			Bdth.alert('警告', '未选中记录.', 'messager-warning');
			return false;

		},
		checkSelectOne: function (rows) {// 检查grid是否只勾选了一行,是返回 true,否返回true
			var records = rows;
			if (!Utils.checkSelect(records)) {
				return false;
			}
			if (records.length == 1) {
				return true;
			}
			Bdth.alert('警告', '只能选择一行记录.', 'messager-warning');
			return false;
		}
	}

	// 自定义事件
	var evt = config.event || {};

	// 默认事件
	var Events = {
		// serach 查询事件
		search: evt.search || Handler.search,
		// add按钮事件
		add: evt.add || Handler.add,
		// edit 按钮事件
		edit: evt.edit || Handler.edit,
		// 刷新Grid 数据
		refresh: evt.refresh || Handler.refresh,
		// 删除记录
		remove: evt.remove || Handler.remove,
		// 保存调用方法
		save: evt.save || Handler.save,
		// 关闭按钮事件
		close: evt.close || Handler.close
	}

	// 按钮控制 btnType 用来控制按钮是否显示,后台根据授权控制是否显示
	var bar_add = {
		id: 'btnadd',
		text: '添加',
		iconCls: 'fa fa-plus',
		btnCls: 'topjui-btn btnmr',
		btnType: 'add',
		handler: Events.add
	};
	var bar_edit = {
		id: 'btnedit',
		text: '修改',
		iconCls: 'fa fa-pencil',
		btnCls: 'topjui-btn-normal btnmr',
		btnType: 'edit',
		handler: Events.edit
	};
	var bar_remove = {
		id: 'btnremove',
		text: '删除',
		iconCls: 'fa fa-trash',
		btnCls: 'topjui-btn-danger',
		btnType: 'remove',
		handler: Events.remove
	};
	var toolbarConfig = [bar_add, bar_edit, bar_remove];
	var getToolbar = function () {
		var tbars = [];
		if (dataGrid.toolbar != undefined && dataGrid.toolbar.length > 0) {
			for (var i = 0; i < dataGrid.toolbar.length; i++) {
				var bar = dataGrid.toolbar[i];
				if (!bar) {
					continue;
				}
				if (bar.btnType == 'add') {
					tbars.push({
						id: bar.id || bar_add.id,
						text: bar.text || bar_add.text,
						iconCls: bar.iconCls || bar_add.iconCls,
						btnCls: bar.btnCls || bar_add.btnCls,
						btnType: bar.btnType || bar_add.btnType,
						handler: bar.handler || bar_add.handler
					});
					continue;
				}
				if (bar.btnType == 'edit') {
					tbars.push({
						id: bar.id || bar_edit.id,
						text: bar.text || bar_edit.text,
						iconCls: bar.iconCls || bar_edit.iconCls,
						btnCls: bar.btnCls || bar_edit.btnCls,
						btnType: bar.btnType || bar_edit.btnType,
						handler: bar.handler || bar_edit.handler
					});
					continue;
				}
				if (bar.btnType == 'remove') {
					tbars.push({
						id: bar.id || bar_remove.id,
						text: bar.text || bar_remove.text,
						iconCls: bar.iconCls || bar_remove.iconCls,
						btnCls: bar.btnCls || bar_remove.btnCls,
						btnType: bar.btnType || bar_remove.btnType,
						handler: bar.handler || bar_remove.handler
					});
					continue;
				}
				tbars.push({
					id: bar.id,
					text: bar.text,
					iconCls: bar.iconCls,
					btnCls: bar.btnCls,
					btnType: bar.btnType,
					handler: bar.handler,
					disabled: bar.disabled
				});
			}
		} else {
			tbars = {};
		}
		return tbars;
	}

	// 初始化表格
	var initGrid = function () {
		var dataconfig = {
			title: dataGrid.title || '数据列表',
			iconCls: dataGrid.iconCls || 'fa fa-save',
			fit: true,
			border: false,
			fitColumns: true,
			nowrap: true,
			autoRowHeight: false,
			striped: false,
			collapsible: false,
			remoteSort: false,
			pagination: true,
			rownumbers: true,
			singleSelect: false,
			checkOnSelect: false,
			selectOnCheck: false,
			pageSize: dataGrid.pageSize || 20,
			pageList: dataGrid.pageList || [20, 50, 100],
			url: dataGrid.url,
			method: dataGrid.method || 'post',
			loadMsg: dataGrid.loadMsg || '加载中 ...',
			queryParams: dataGrid.queryParams || {},
			idField: dataGrid.idField,
			columns: dataGrid.columns,
			//toolbar: getToolbar(),
			onLoadSuccess: dataGrid.onLoadSuccess || function () {
				Grid.datagrid('unselectAll');
				Grid.datagrid('uncheckAll');
			},
			onSelect: function (rowIndex, rowData) {
				// 选择一行
				var rows = Grid.datagrid('getRows');
				$.each(rows, function (i) {
					if (i != rowIndex) {
						Grid.datagrid('uncheckRow', i);
						Grid.datagrid('unselectRow', i);
					}
				})
				Grid.datagrid('checkRow', rowIndex);
			}
		};
		Grid.datagrid(dataconfig);
	}
	// 初始化Grid按钮 按钮控制
	var initTbar = function () {
		var tbars = getToolbar();
		Grid.datagrid({'toolbar': tbars});
	}

	// 初始话form
	var initForm = function () {
		if (Form.search && Form.search[0]) {
			Form.search.find("#btn-search").click(Events.search); // 保存事件
			$("#btnadd").click(Events.add);
			$("#btnedit").click(Events.edit);
			$("#btndelete").click(Events.remove);
		}
	}

	// this 返回属性
	this.win = Win;
	this.form = Form;
	this.grid = Grid;
	this.utils = Utils;
	this.handler = Handler;

	// 初始化方法
	this.init = function () {
		initGrid();
		//initTbar();
		initForm();
		//initWin();
	}
	// 调用初始化
	return this;
};